package com.logikas.mobile;

import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class ConvertTemperatureActivity extends Activity {
	
	private EditText text; 
	
	private static final int REQUEST_CODE = 1;
	private Bitmap bitmap;
	private ImageView imageView;
	private Camera camera;
	private int numCameras;
	int cameraCurrentlyLocked;
    int defaultCameraId;
    SurfaceHolder mSurfaceHolder;
    SurfaceView mSurfaceView;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        text = (EditText) findViewById(R.id.editText1);
        
        numCameras = Camera.getNumberOfCameras();
        CameraInfo cameraInfo = new CameraInfo();
        for (int i = 0; i < numCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            
            Log.v("ConvertTemperature", String.valueOf(cameraInfo.facing ));
            
            if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
                defaultCameraId = i;
            }
        }
        
        mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
        mSurfaceHolder = mSurfaceView.getHolder();
        
        camera = Camera.open(defaultCameraId);
        
        if ( camera == null ){
        	((TextView)findViewById(R.id.text_error)).setText(R.string.camera_error);
        }else{
        	try {
    			camera.setPreviewDisplay(mSurfaceHolder);
    			camera.startPreview();
    			
    			((TextView)findViewById(R.id.text_error)).setText("Camera configured");
    		} catch (IOException e) {
    			((TextView)findViewById(R.id.text_error)).setText(R.string.camera_error);
    		} 
        }
    }
    
        
    public void customClickHandler(View view) {
		switch (view.getId()) {
		
		case R.id.button1:
			RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
			RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
			if (text.getText().length() == 0) {
				Toast.makeText(this, "Please enter a valid number",
						Toast.LENGTH_LONG).show();
				return;
			}

			float inputValue = Float.parseFloat(text.getText().toString());
			if (celsiusButton.isChecked()) {
				text.setText(String
						.valueOf(convertFahrenheitToCelsius(inputValue)));
				celsiusButton.setChecked(false);
				fahrenheitButton.setChecked(true);
			} else {
				text.setText(String
						.valueOf(convertCelsiusToFahrenheit(inputValue)));
				fahrenheitButton.setChecked(false);
				celsiusButton.setChecked(true);
			}
			break;
		}
	}

	// Converts to celsius
	private float convertFahrenheitToCelsius(float fahrenheit) {
		return ((fahrenheit - 32) * 5 / 9);
	}

	// Converts to fahrenheit
	private float convertCelsiusToFahrenheit(float celsius) {
		return ((celsius * 9) / 5) + 32;
	}

    
}